package ca.kooki.codesnippet;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

import ca.kooki.codesnippet.core.ActionResponse;

public class CodeSnippetPreferencePage extends PreferencePage implements IWorkbenchPreferencePage 
{

  //```````````````````````
  // STATIC VARIABLES
  //```````````````````````
  
  public static final String REPOSITORY_FILE_PREFERENCE = "repository_file";
  public static final String USER_PREFERENCE = "user";
  
  public static final String DEFAULT_REPOSITORY_FILE_PREFERENCE = ".\\code-snippet-repository.xml";
    
  //```````````````````````
  // MEMBER VARIABLES
  //```````````````````````
  
  public Label labelCodeSnippetFile;
  public Text inputCodeSnippetFile;

  public Label labelUser;
  public Text inputUser;
  
  public Label labelStatusTitle;
  public Label labelStatus;

  
	protected Control createContents(Composite parent) 
  {
    
    Composite preference = createPreferenceComposite(parent);
    
    labelCodeSnippetFile = createLabel(preference,"Repository Location:");
    inputCodeSnippetFile = new Text(preference,SWT.LEFT | SWT.SINGLE | SWT.BORDER);
    
    labelUser = createLabel(preference,"Logged In As:");
    inputUser = new Text(preference,SWT.LEFT | SWT.SINGLE | SWT.BORDER);
    
    labelStatusTitle = createLabel(preference,"");
    labelStatus = new Label(preference,SWT.LEFT | SWT.WRAP );
    
		GridData data = new GridData(GridData.FILL_HORIZONTAL);
    inputCodeSnippetFile.setLayoutData(data);
    inputUser.setLayoutData(data);

    data = new GridData(GridData.FILL_VERTICAL);
    labelStatus.setLayoutData(data);

    
    initialPreferences();
    return preference;
	}

	/*
	 * @see IWorkbenchPreferencePage#init(IWorkbench)
	 */
	public void init(IWorkbench workbench) {
		setPreferenceStore(Activator.getDefault().getPreferenceStore());
	}

	protected void performDefaults() {
		inputCodeSnippetFile.setText(Activator.getDefault().getPreferences().getDefault(REPOSITORY_FILE_PREFERENCE));
    inputUser.setText(Activator.getDefault().getPreferences().getDefault(USER_PREFERENCE));
    updateStatus();
	}

	public boolean performOk() 
  {
    Activator.getDefault().getPreferences().setValue(REPOSITORY_FILE_PREFERENCE,inputCodeSnippetFile.getText());
    
    String username = inputUser.getText();
    Activator.getDefault().getPreferences().setValue(USER_PREFERENCE,username);
    updateStatus();
    return super.performOk();
	}

  public void initialPreferences()
  {
    inputCodeSnippetFile.setText(Activator.getDefault().getPreferences().getString(REPOSITORY_FILE_PREFERENCE));
    String username = Activator.getDefault().getPreferences().getString(USER_PREFERENCE);
    inputUser.setText(username);
    updateStatus();
  }

  //```````````````````````
  // PRIVATE METHODS
  //```````````````````````
  
  private Composite createPreferenceComposite(Composite parent)
  {
    Composite preference = new Composite(parent, SWT.NULL);

    GridData data = new GridData(GridData.FILL_HORIZONTAL);
    data.grabExcessHorizontalSpace = true;
    preference.setLayoutData(data);
    
    GridLayout layout = new GridLayout();
    layout.numColumns = 2;
    preference.setLayout(layout);     
    
    return preference;
  }
  
  private Label createLabel(Composite parent, String text)
  {
    Label label = new Label(parent,SWT.RIGHT | SWT.TOP);
    label.setText(text);
    return label;
  }
  
  private void updateStatus()
  {
    ActionResponse response = Activator.getDefault().getRepository().isValid();
    labelStatus.setText(response.getMessage());
    
  }



	

}